package main.java;

import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.resource.Resource;
import org.sailing.cim.decoder.Decoder;

import java.io.File;
import java.io.FileInputStream;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Wang Bin
 *         Date: 12-6-26
 */
public class CIMUpdator {
    private static Logger log = Logger.getLogger(CIMUpdator.class);

    public static IResourceManager update(String file) {
        try {
            File f = new File(file);
            if (!f.exists()) {
                log.warn("No cim file is found :" + f.getPath());
                return null;
            }
            log.info("Parsing cim xml start.");
            Resource resource = Decoder.load(new FileInputStream(f));
            IResourceManager resourceManager = new DefaultResourceManager(resource);
            log.info("CIM data is updated from file :" + file);
            log.info("Parsing cim xml end.");
            return resourceManager;
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("Exception occur when updating cim from cim:" + e.getMessage());
            return null;
        }
    }
}
